package com.googlecode.monitoring.model;

import org.joda.time.DateTime;

import java.util.Set;

/**
 * Date: Jul 29, 2010
 * Time: 2:59:12 PM
 */
public abstract class ItemToCheck implements Comparable<ItemToCheck>{
    protected String id;
    protected String name;
    protected String descr;
    protected StateName currentStateName = StateName.NEW;
    protected DateTime lastCheckTime;
    protected DateTime nextCheckTime;
    protected EventTime eventTime;
    protected Set<Person> responsiblePersons;

    public DateTime getNextCheckTime() {
        return nextCheckTime;
    }

    public void setNextCheckTime(DateTime nextCheckTime) {
        this.nextCheckTime = nextCheckTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescr() {
        return descr;
    }

    public void setDescr(String descr) {
        this.descr = descr;
    }

    public StateName getCurrentStateName() {
        return currentStateName;
    }

    public void setCurrentStateName(StateName cur) {
        this.currentStateName = cur;
    }

    public DateTime getLastCheckTime() {
        return lastCheckTime;
    }

    public void setLastCheckTime(DateTime lastCheckTime) {
        this.lastCheckTime = lastCheckTime;
    }

    public EventTime getEventTime() {
        return eventTime;
    }

    public void setEventTime(EventTime eventTime) {
        this.eventTime = eventTime;
    }

    public Set<Person> getResponsiblePersons() {
        return responsiblePersons;
    }

    public void setResponsiblePersons(Set<Person> responsiblePersons) {
        this.responsiblePersons = responsiblePersons;
    }

    /**
     * use for switch state during working of system
     *
     * @param stateName state
     */
    public void switchState(StateName stateName) {
        if (stateName == StateName.CHECKED) {
            if (currentStateName == StateName.NEW || currentStateName == StateName.EMAIL_SENT) {
                currentStateName = StateName.CHECKED;
            }
        } else if (stateName == StateName.EMAIL_SENT) {
            if (currentStateName == StateName.CHECKED || currentStateName == StateName.NEW) {
                currentStateName = StateName.EMAIL_SENT;
            }
        } else {
            throw new IllegalStateException("state 'email sent' must be only after state 'checked'");
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ItemToCheck that = (ItemToCheck) o;

        if (id != null ? !id.equals(that.id) : that.id != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("ItemToCheck");
        sb.append("{id='").append(id).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", descr='").append(descr).append('\'');
        sb.append(", currentState=").append(currentStateName);
        if(lastCheckTime != null)
        sb.append(", lastCheckTime=").append(lastCheckTime);
        if(nextCheckTime != null)
        sb.append(", nextCheckTime=").append(nextCheckTime);
        sb.append(", eventTime=").append(eventTime);
        sb.append(", responsiblePersons=").append(responsiblePersons);
        sb.append('}');
        return sb.toString();
    }

    @Override
    public int compareTo(ItemToCheck o) {
        return id.compareToIgnoreCase(o.getId());
    }
}
